const path = require('path')

const basePath = dir => path.join(__dirname, dir)
const CompressionPlugin = require('compression-webpack-plugin')
module.exports = {
  productionSourceMap: false,
  chainWebpack: config => {
    config.resolve.alias.set('@', basePath('src'))
    config.when(process.env.NODE_ENV === 'production', config => {
      config.entry('app').clear().add('./src/main-pro.js')
      config.set('externals', {
        vue: 'Vue',
        'router': 'VueRouter',
        axios: 'axios',
        lodash:'_',
        echarts: 'echarts',
        nprogress: 'NProgress',
        'vue-quill-editor': 'VueQuillEditor',
        jszip: 'JSZip',
        xlsx: 'XLSX',
        html2canvas: 'html2canvas',
        jspdf: 'jsPDF',
        sortablejs: 'Sortable',
        'vue-i18n': 'VueI18n',
      })
      config.plugin('html').tap(args => {
        args[0].isProd = true
        return args
      })
      return {
        plugins: [new CompressionPlugin({
          test: /\.js$|\.html$|\.css$/gi,
          threshold: 10240,
          deleteOriginalAssets: false
        })]
      }
    })
    config.when(process.env.NODE_ENV === 'development', config => {
      config.entry('app').clear().add('./src/main-dev.js')
      config.plugin('html').tap(args => {
        args[0].isProd = false
        return args
      })
    })
  }
}
